'use strict';

const koajwt = require('koa-jwt2');

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller, jwt } = app;
  router.get('/', controller.home.index);

  router.get('/get-config', controller.home.getAppConfig);
  router.post('/login', controller.home.login);
  router.get('/user-info', jwt, controller.home.userInfo);
  const isRevokedAsync = function (req, payload) {
    return new Promise(resolve => {
      try {
        const userId = payload.id;
        const tokenKey = `token_${userId}`;
        const token = app.redis.get(tokenKey);
        if (token) {
          app.redis.del(tokenKey);
        }
        resolve(false);
      } catch (e) {
        resolve(true);
      }

    });
  };
  router.post('/logout', koajwt({
    secret: app.config.jwt.secret,
    credentialsRequired: false,
    isRevoked: isRevokedAsync,
  }), controller.home.logout);

  router.get('/authing/auth', controller.home.authingAuth);


  router.resources('roles', '/roles', jwt, controller.role);
  router.resources('users', '/users', jwt, controller.user);
  router.resources('posts', '/posts', jwt, controller.post);

  /* 上传文件 */
  router.post('/upload', controller.upload.upload);
  router.get('/cleanFile', controller.upload.cleanFile);
  router.get('/open', controller.upload.open);

  /* 微博相关api */
  router.post('/wbNameList', controller.wbUrl.index); // 列表
  router.delete('/wbNameDelete/:id', controller.wbUrl.destroy); // 删除
  router.get('/wbSelectName', controller.wbUrl.selectName); // 查询
  router.post('/wbNameCreate', controller.wbUrl.create); // 添加
  router.post('/wbUpdateById', controller.wbUrl.update); // 更新
  router.get('/wbList', controller.wbUrl.wbList); // 获取发送列表
  router.post('/wbSend', controller.wbUrl.wbSend); // 发送微博
  router.get('/wbDelete', controller.test.wbDelete); // 删除微博

  router.get('/test', controller.test.test);
};
